/*
 * Jamus
 * Copyright (C) 2010 Jamus Team
 *
 * Jamus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Jamus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Jamus.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __BACKEND_STUB_PLUGIN
#define __BACKEND_STUB_PLUGIN

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>

#include "../Plugin.h"

namespace backend {
namespace stub {

class Plugin : public backend::Plugin, private boost::noncopyable
{
public:
	/**
	 * Constructs the stub. Initial state:
	 * Track list and playlist are empty. Current track is not set. Playback is
	 * stopped. Volume is 50.
	 */
	Plugin();

	//! Constructs the stub with the specified initial state
	Plugin(const TrackList& tracks, const Playlist& playlist, PlaybackStatus playbackStatus, int volume, int currentTrack = -1);

	virtual ~Plugin();

	virtual void run();
	virtual void cancel();

	// ************************************************************************
	// The actual back-end interface starts here
	// ************************************************************************

	/*
	 * Playback
	 */
	virtual PlaybackStatus getPlaybackStatus();
	virtual void play();
	virtual void play(int track);
	virtual void pause();
	virtual void stop();
	virtual void playPause();
	virtual void nextTrack();
	virtual void prevTrack();
	virtual int getCurrentTrack();
	virtual boost::posix_time::time_duration getElapsed();
	virtual void seek(const boost::posix_time::time_duration& pos);

	/*
	 * Mixer
	 */
	virtual int getVolume();
	virtual void setVolume(int vol);

	/*
	 * Music library
	 */
	virtual const TrackList& getTrackList();

	/*
	 * Current playlist
	 */
	virtual const Playlist& getPlaylist();
	virtual void addToPlaylist(const std::string& fileName);
	virtual void removeFromPlaylist(int index);
	virtual void clearPlaylist();

protected:
	void resetElapsed();

	TrackList tracks;
	Playlist playlist;
	PlaybackStatus playbackStatus;
	int currentTrack;
	int volume;
	boost::posix_time::ptime timeOfLastElapsedUpdate;
	boost::posix_time::time_duration elapsed;

	// Used so the thread calling interface methods can notify the run thread of signals to fire
	boost::condition_variable fireSignals;
	std::set< boost::signals2::signal<void ()>* > signalsToFire;
	bool running;
	boost::mutex mutex;

	void scheduleSignal(boost::signals2::signal<void ()>& sig);
};

} // namespace stub
} // namespace backend

#endif
